package foo.bar.wiki.web.action.page;

import foo.bar.wiki.web.action.BaseActionBean;
import foo.bar.wiki.web.extension.PageTypeConverter;
import foo.bar.wiki.web.nonextension.NullResolution;
import foo.bar.wiki.domain.Page;
import foo.bar.wiki.domain.Comment;
import foo.bar.wiki.services.WikiService;
import foo.bar.wiki.services.exception.ThemeNotAvailableException;
import foo.bar.wiki.services.exception.ThemeMessageNotAvailableException;
import foo.bar.wiki.radeox.DefaultWikiRenderEngine;
import foo.bar.wiki.plugins.PluginException;
import foo.bar.radeox.api.engine.RenderEngine;
import foo.bar.radeox.engine.context.BaseRenderContext;
import net.sourceforge.stripes.action.*;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.integration.spring.SpringBean;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author tmjee
 * @version $Date$ $Id$
 */
public class PageActionBean extends BaseActionBean {

    private static final Log LOG = LogFactory.getLog(PageActionBean.class);


    public static final String VIEW = "/page/page.jsp";
    public static final String THEME_NOT_AVAILABLE_VIEW = "/page/theme_not_available.jsp";
    public static final String THEME_MESSAGE_NOT_AVAILABLE_VIEW = "/page/theme_message_not_available.jsp";

    private String renderedContent;

    private RenderEngine renderEngine;

    private String wikiMarkup;

    private WikiService wikiService;

    @Validate(converter = PageTypeConverter.class)
    private Page page;


    @DefaultHandler
    public Resolution view() {
        try {
            return new ForwardResolution(
                generateThemePath(page.getSpace().getThemeName(),
                page.getPageLocation()));
        }
        catch(ThemeNotAvailableException e) {
            return new ForwardResolution(generateThemePath(THEME_NOT_AVAILABLE_VIEW));
        }
        catch(ThemeMessageNotAvailableException e) {
            return new ForwardResolution(generateThemePath(THEME_MESSAGE_NOT_AVAILABLE_VIEW));
        }
    }

    public Resolution preparePageContents() {
        BaseRenderContext renderContext = new BaseRenderContext();
        System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& put "+page.getSpace().getId()+" into RENDER_CONTEXT_SPACE_ID_PARAM_KEY");
        renderContext.getParameters().put(DefaultWikiRenderEngine.RENDER_CONTEXT_SPACE_ID_PARAM_KEY, page.getSpace().getId());
        renderedContent = renderEngine.render(page.getContent(), renderContext);
        for (Comment comment : page.getComments()) {
            comment.setRenderedContent(renderEngine.render(comment.getContent()));
        }
        return new NullResolution();
    }


    public Resolution save() throws PluginException {
        wikiService.updatePage(page);
        getContext().getMessages().add(new SimpleMessage("Page with id=["+page.getId()+"] and name=["+page.getName()+"] saved successfully"));
        return new RedirectResolution(PageActionBean.class, "view").addParameter("page", page.getId());
    }


    @SpringBean("wikiService")
    public void setWikiService(WikiService wikiService) {
        this.wikiService = wikiService;
    }





    public Resolution renderWikiMarkup() {
        return new StreamingResolution("text/html"){
            protected void stream(HttpServletResponse httpServletResponse) throws Exception {
                BaseRenderContext renderContext = new BaseRenderContext();
                renderContext.getParameters().put(DefaultWikiRenderEngine.RENDER_CONTEXT_SPACE_ID_PARAM_KEY, page.getSpace().getId());
                String result = renderEngine.render(wikiMarkup, renderContext);
                if (result != null) {
                    httpServletResponse.setContentLength(result.getBytes().length);
                    httpServletResponse.getOutputStream().write(result.getBytes());
                }
            }
        };
    }


    @SpringBean("renderEngine")
    public void setRenderEngine(RenderEngine renderEngine)  {
        this.renderEngine = renderEngine;
    }

    public Page getPage() {
        return page;
    }

    public void setPage(Page page) {
        this.page = page;
    }

    public String getRenderedContent() {
        return renderedContent;
    }

    public void setRenderedContent(String renderedContent) {
        this.renderedContent = renderedContent;
    }

    public String getWikiMarkup() {
        return wikiMarkup;
    }

    public void setWikiMarkup(String wikiMarkup) {
        this.wikiMarkup = wikiMarkup;
    }
}
